using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace GraphAlgsPresentation.Core
{
    /// <summary>
    /// The class provides information of the context, including layout and comment.
    /// </summary>
    [Serializable]
    public class Context
    {
        private LayoutContext layout = new LayoutContext();
        private CommentControl comment;

        #region Properties

        /// <summary>
        /// Gets or sets the layout context.
        /// </summary>
        /// <value>The layout context.</value>
        public LayoutContext LayoutContext
        {
            get { return layout; }
            set { layout = value; }
        }

        /// <summary>
        /// Gets or sets the comment control.
        /// </summary>
        /// <value>The comment control.</value>
        [XmlIgnore]
        public CommentControl CommentControl
        {
            get { return comment; }
            set { comment = value; }
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Gets the vertex.
        /// </summary>
        /// <param name="VertexName">Name of the vertex.</param>
        /// <returns></returns>
        public LCVertex GetVertex(String VertexName)
        {
            return layout.GetVertex(VertexName);
        }

        /// <summary>
        /// Gets the edge.
        /// </summary>
        /// <param name="VertexName1">The name of the first vertex.</param>
        /// <param name="VertexName2">The name of the second vertex.</param>
        /// <returns></returns>
        public LCEdge GetEdge(String VertexName1, String VertexName2)
        {
            return layout.GetEdge(VertexName1, VertexName2);
        }

        /// <summary>
        /// Updates this context.
        /// </summary>
        public void Update()
        {
            foreach (LCVertex vertex in LayoutContext.Vertices)
                vertex.Updated = vertex.Marked = false;
            foreach (LCEdge edge in LayoutContext.Edges)
                edge.Updated = edge.Marked = false;
        }

        #endregion

        #region Graphics method

        /// <summary>
        /// Draws the unchanged items.
        /// </summary>
        /// <param name="g">The graphics.</param>
        public void DrawUnchangedItems(Graphics g)
        {
            foreach (LCVertex vertex in LayoutContext.Vertices)
                if (!vertex.Updated) vertex.Draw(g);
            foreach (LCEdge edge in LayoutContext.Edges)
                if (!edge.Updated) edge.Draw(g);
        }

        /// <summary>
        /// Draws the changed items.
        /// </summary>
        /// <param name="g">The graphics.</param>
        /// <param name="alpha">The alpha.</param>
        public void DrawChangedItems(Graphics g, int alpha)
        {
            foreach (LCVertex vertex in LayoutContext.Vertices)
                if (vertex.Updated) vertex.DrawAlpha(g, alpha);
            foreach (LCEdge edge in LayoutContext.Edges)
                if (edge.Updated) edge.DrawAlpha(g, alpha);
        }

        #endregion
    }
}
